<template>
  <div class="list_container flex both-100">
    <Breadcrumb :title="pageTitle" />
    <div class="content_container">
      <!-- 搜索模块 -->
      <el-form ref="searchMap" :inline="true" :model="searchMap" class="serach_wrapper flex-space-between">
        <div class="search">
          <el-form-item prop="ipaddr" size="small">
            <el-input v-model="searchMap.ipaddr" placeholder="请输入登录地址" clearable @keyup.enter.native="searchMap" />
          </el-form-item>
          <el-form-item prop="userName" size="small">
            <el-input v-model="searchMap.userName" placeholder="请输入用户名称" clearable @keyup.enter.native="searchMap" />
          </el-form-item>
          <el-form-item class="flex">
            <el-button @click="handleSearch()" class="btn_primary">查询</el-button>
            <el-button @click="handleReset()" class="btn_cancel">重置</el-button>
          </el-form-item>
        </div>
      </el-form>
      <!-- 搜索模块over -->

      <!-- 表格模块 -->
      <div class="table_container">
        <el-table v-loading="loading" :data="onlineList.slice((pageNum-1)*pageSize,pageNum*pageSize)" @selection-change="handleSelectionChange">
          <el-table-column label="序号" type="index" align="center">
            <template slot-scope="scope">
              <span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
            </template>
          </el-table-column>
          <el-table-column label="会话编号" align="center" prop="tokenId" :show-overflow-tooltip="true" />
          <el-table-column label="登录名称" align="center" prop="userName" :show-overflow-tooltip="true" />
          <el-table-column label="主机" align="center" prop="ipaddr" :show-overflow-tooltip="true" />
          <el-table-column label="登录时间" align="center" prop="loginTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.loginTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <span type="text" class="tag_warning" size="small" @click="handleRetreat(scope.row)">强退</span>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!-- 分页 -->
      <pagination v-show="total > 0" :total="total" :page.sync="searchMap.pageNum" :limit.sync="searchMap.pageSize" @change="getList"></pagination>
      <!-- 分页over -->
    </div>
  </div>
</template>

<script>
import { list, forceLogout } from "@/api/monitor/online";
export default {
  name: "notice",
  dicts: ['sys_notice_status', 'sys_notice_type'],
  data () {
    return {
      // 加载状态
      loading: false,
      //搜索条件
      searchMap: {
        ipaddr: undefined,
        userName: undefined
      },
      onlineList: [],
      pageNum: 1,
      pageSize: 10,
      // 总条数
      total: 0,
      // 弹框状态 

    };
  },
  computed: {
    pageTitle () {
      return '在线用户'
    }
  },
  created () {
    this.getList()
  },
  mounted () {

  },
  watch: {},
  methods: {
    getList () {
      this.loading = true;
      list(this.searchMap).then(res => {
        this.onlineList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    // 强退按钮
    handleRetreat (row) {
      this.$confirm('是否确认强退名称为"' + row.userName + '"的用户？').then(function () {
        return forceLogout(row.tokenId);
      }).then(() => {
        this.getList();
        this.$message.success("强退成功");
      })
    },
    /** 搜索按钮操作 */
    handleSearch () {
      this.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    handleReset () {
      this.$nextTick(() => {
        this.$refs['searchMap'].resetFields();
        this.handleSearch();
      })
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.dialogVisibleNotice = true;
      this.title = "添加公告";
    },
    /** 修改按钮操作 */
    handleEdit (row) {
      this.reset();
      const noticeId = row.noticeId || this.ids
      getNotice(noticeId).then(res => {
        this.formModel = res.data;
        this.dialogVisibleNotice = true;
        this.title = "修改公告";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.formModel.noticeId != undefined) {
            updateNotice(this.formModel).then(res => {
              if (200 === res.code) {
                this.$message.success("修改成功");
                this.dialogVisibleNotice = false;
                this.getList();
              }
            });
          } else {
            addNotice(this.formModel).then(res => {
              if (200 === res.code) {
                this.$message.success("新增成功");
                this.dialogVisibleNotice = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const noticeIds = row.noticeId || this.ids
      this.$confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？').then(function () {
        return delNotice(noticeIds);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      })
    }
  },
}
</script>

<style lang='less' scoped>
</style>